package com.jx.lcs.customer.service;

import com.jx.lcs.customer.dao.CustomerDao;
import com.jx.lcs.customer.entity.Customer;
import com.tianyu.jty.common.persistence.HibernateDao;
import com.tianyu.jty.common.persistence.Page;
import com.tianyu.jty.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;

/** 
 * 
 * @author JiaYY 
 * @date 2015年7月15日
 */
@Service
@Transactional(readOnly=true)
public class CustomerService extends BaseService<Customer, String> {

    @Autowired
    private CustomerDao customerDao;

    @Override
    public HibernateDao<Customer, String> getEntityDao() {
        return customerDao;
    }

    public Page<Customer> findCustomerList(Page<Customer> page, HttpServletRequest request) throws Exception {
        String userName = request.getParameter("userName");
        String uid = request.getParameter("uid");
        String lcsCode = request.getParameter("lcsCode");
        String lcsName = request.getParameter("lcsName");
        //String city = request.getParameter("city");
        String telephone = request.getParameter("telephone");
        String beginDate = request.getParameter("beginDate");
        String endDate = request.getParameter("endDate");
        String idCardNO = request.getParameter("idCardNO");

        StringBuilder sb = new StringBuilder();
        sb.append("from Customer t where 1=1");

        if(uid != null && !"".equals(uid.trim())) {
            sb.append(" and t.uid = '").append(uid).append("'");
        }
        if(lcsCode != null && !"".equals(lcsCode.trim())) {
            sb.append(" and t.userInfo.uid = '").append(lcsCode).append("'");
        }
        if(lcsName != null && !"".equals(lcsName.trim())) {
        	lcsName = URLDecoder.decode(lcsName, "UTF-8");
            sb.append(" and t.userInfo.userName = '").append(lcsName).append("'");
        }
        if(idCardNO != null && !"".equals(idCardNO.trim())) {
            sb.append(" and t.idCardNO = '").append(idCardNO).append("'");
        }
        if(userName != null && !"".equals(userName.trim())) {
            userName = URLDecoder.decode(userName, "UTF-8");
            sb.append(" and t.trueName like '%").append(userName).append("%'");
        }
//        if(city != null && !"".equals(city.trim())) {
//            city = URLDecoder.decode(city, "UTF-8");
//            sb.append(" and t.city like '%").append(city).append("%'");
//        }
        if(telephone != null && !"".equals(telephone.trim())) {
            telephone = URLDecoder.decode(telephone, "UTF-8");
            sb.append(" and t.userTel like '%").append(telephone).append("%'");
        }
        if(beginDate != null && !"".equals(beginDate.trim())) {
            sb.append(" and t.recordTime > '").append(beginDate).append(" 00:00:01'");
        }
        if(endDate != null && !"".equals(endDate.trim())) {
            sb.append(" and t.recordTime <= '").append(endDate).append(" 23:59:59'");
        }

        sb.append(" order by t.recordTime desc");
        return customerDao.findPage(page, sb.toString(), new Object[]{});
    }
}
